package com.iconmaker.controller;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bckim.iconmaker.R;
import com.iconmaker.model.Conf;

public class GridAdapter extends BaseAdapter {
	
	private String[] imageList;
	private Context context;
	private String rootPath;
	
	public GridAdapter(GalleryActivity galleryActivity, String[] list) {
		imageList = list;
		context = galleryActivity;
		rootPath = Conf.rootPath + context.getPackageName() + "/";
	}
	
	@Override
	public int getCount() {
		return imageList != null ? imageList.length + 1 : 1;
	}
	
	@Override
	public Object getItem(int position) {
		return position == 0 ? R.drawable.add : imageList[position - 1];
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null) {
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.grid_image, null);
		}
		
		if(position == 0)
			((ImageView)(convertView.findViewById(R.id.imageView))).setBackgroundResource(R.drawable.add);
		else {
			((ImageView)(convertView.findViewById(R.id.imageView))).setBackgroundDrawable(new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(BitmapFactory.decodeFile(rootPath + imageList[position - 1]), Conf.countOfColumnPixcel * 3, Conf.countOfRowPixcel * 3, false)));
		}
		
		return convertView;
	}
	
	public void setDataList(String[] imageList) {
		this.imageList = imageList;
	}
}
